$(function() {
	//得到省份
	$.ajax({
		url: './xml/province.xml',
		dataType: 'xml',
		success: function(data) {
			$(data).find("fields").each(function() {
				var fields = $(this);
				var provinces = fields.find("province");
				for (var i = 0; i < provinces.length; i++) {
					var province = $(provinces[i]).text();
					var objNewOption = $('<option>', {
						'id': 'i',
						'name': 'pro',
						'value': $(provinces[i]).attr('name')
					});
					objNewOption.html(province);
					$('[name="province"]').append(objNewOption);
				};
			})
		}
	});
	//得到城市
	var provinceName;
	$('[name="province"]').change(function() {
		$('[name="area"]').html('<option selected="true" value=""></option>');
		provinceName = $(this).val();
		if (provinceName) {
			$.ajax({
				url: './xml/province/' + provinceName + '.xml',
				dataType: 'xml',
				success: function(data) {
					$(data).find("fields").each(function() {
						var fields = $(this);
						var citys = $(fields).find('city');
						$('[name="city"]').html('<option selected="true" value=""></option>');
						for (var i = 0; i < citys.length; i++) {
							var city = $(citys[i]).text();
							var objNewOption = $('<option>', {
								'id': 'i',
								'value': city
							});
							objNewOption.html(city);
							$('[name="city"]').append(objNewOption);
						};
					})
				}
			})
		} else {
			$('[name="city"]').html('<option selected="true" value=""></option>');
		}
	});

	//得到地区
	var center;
	$('[name="city"]').change(function() {
		cityName = $(this).val();
		if (cityName) {
			$.ajax({
				url: './xml/city/' + provinceName + '.xml',
				dataType: 'xml',
				success: function(data) {
					$(data).find("fields").each(function() {
						var fields = $(this);
						var city = fields.find('city[name=' + cityName + ']');
						center = {
							X: $(city).attr('centerX'),
							Y: $(city).attr('centerY')
						};
						console.log(center);
						var areas = $(city).find('area');
						$('[name="area"]').html('<option selected="true" value=""></option>');
						for (var i = 0; i < areas.length; i++) {
							var area = $(areas[i]).text();
							var objNewOption = $('<option>', {
								'id': 'i',
								'value': area
							});
							objNewOption.html(area);
							$('[name="area"]').append(objNewOption);
						};
					})
				}
			})
		} else {
			$('[name="area"]').html('<option selected="true" value=""></option>');
		}
	});


	//添加经营范围
	$('[name="type"]').click(function() {
		if ($('[name="type"]:checked').val() == "more") {
			$('[name="add_type"]').removeAttr('disabled');
		} else {
			$('[name="add_type"]').attr('disabled', 'disabled');
		}
	});

	//获取表单数据
	$('[name="submit"]').click(function() {
		var clause = $('[name="clause"]:checked').val();
		var username = $('[name="username"]').val();
		var realname = $('[name="realname"]').val();
		var password = get_pwd();
		var activation_code = $('[name="activation_code"]').val();
		var storename = $('[name="store_name"]').val();
		var address;
		if ($('[name="detail_addr"]').val()) {
			address = $('[name="pro"]:selected').text() + ',' + $('[name="city"]').val() + ',' + $('[name="area"]').val() + ',' + $('[name="detail_addr"]').val();
		};
		var coordinate = center;
		var email = get_email();
		var phone_num = $('[name="phone_num"]').val();
		var open_time;
		if ($('[name="start_hour"]').val()) {
			open_time = $('[name="start_hour"]').val();
			if ($('[name="start_minute"]').val()) {
				open_time += ':' + $('[name="start_minute"]').val();
				if ($('[name="end_hour"]').val()) {
					open_time += '-' + $('[name="end_hour"]').val();
					if ($('[name="end_minute"]').val()) {
						open_time += ':' + $('[name="end_minute"]').val();
					};
				}
			};
		}
		var type = get_type();
		var sotre_int = $('[name="int"]').val();

		//传送数据
		if (clause && username && realname && password && activation_code && storename && address && coordinate && email && phone_num && open_time && type && sotre_int) {

			$.ajax({
				type: 'POST',
				dataType: "json",
				url: "http://localhost:3000/user",
				success: function(data) {
					console.log(data);
				}
			});

		} else {
			alert("warn",'请完整填写信息！！！');
		}

	});
});

function checkhour(obj) {
	$(obj).val($(obj).val().replace(/\D/g, ''));
	if ($(obj).val() < 0) {
		$(obj).val('0');
	} else if ($(obj).val() > 23) {
		$(obj).val('23');
	}
}

function checkminute(obj) {
	$(obj).val($(obj).val().replace(/\D/g, ''));
	if ($(obj).val() < 0) {
		$(obj).val('0');
	} else if ($(obj).val() > 59) {
		$(obj).val('59');
	}
}

function get_type() {
	if ($('[name="type"]:checked').val() == "more") {
		return $('[name="add_type"]').val();
	} else {
		return $('[name="type"]:checked').val();
	}
}

function get_pwd() {
	if ($('[name="repassword"]').val() != $('[name="password"]').val()) {
		alert("warn",'两次输入密码必须相同！');
	} else {
		return $('[name="password"]').val();
	}
}

function get_email() {
	var myreg = /(\S)+[@]{1}(\S)+[.]{1}(\w)+/;
	if (myreg.test($('[name="email"]').val())) {
		return $('[name="email"]').val();
	} else {
		alert("warn",'请填写合法邮箱地址！');
	}
}